//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.mdiy.tag;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.map.MapWrapper;
import freemarker.core.Environment;
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.template.Configuration;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import java.io.IOException;
import java.sql.Clob;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import net.mingsoft.basic.util.SpringUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.mdiy.biz.ITagBiz;
import net.mingsoft.mdiy.entity.TagEntity;
import net.mingsoft.mdiy.util.ParserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CustomTag implements TemplateDirectiveModel {
    protected static BeansWrapper build;
    protected final Logger LOG = LoggerFactory.getLogger(this.getClass());
    private Map data;
    private TagEntity tag;
    private String variableName;
    private Map tagParams;

    public CustomTag() {
    }

    public CustomTag(Map data, TagEntity tag) {
        this.data = data;
        this.tag = tag;
        this.variableName = "field";
    }

    public Map getTagParams() {
        return this.tagParams;
    }

    public void execute(Environment environment, Map params, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        if (this.data == null && this.tag == null) {
            this.tagParams = params;
        } else {
            HashMap<Object, Object> _params = MapUtil.newHashMap();
            _params.putAll(this.data);
            TemplateModel oldVar = environment.getVariable(this.variableName);
            TemplateModel column = environment.getVariable("column");
            if (column != null) {
                _params.put("column", build.unwrap(column));
            }

            params.forEach((k, v) -> {
                if (v instanceof TemplateModel) {
                    try {
                        _params.put(k, build.unwrap((TemplateModel)v));
                    } catch (TemplateModelException var5) {
                        var5.printStackTrace();
                        this.LOG.error("转换参数错误 key:{} -{}", k, var5);
                    }
                }

            });
            ITagBiz tagSqlBiz = (ITagBiz)SpringUtil.getBean(ITagBiz.class);
            String sql = ParserUtil.rendering(_params, this.tag.getTagSql());
            List<Map> _list = (List)tagSqlBiz.excuteSql(sql);
            AtomicInteger index = new AtomicInteger(1);
            _list.forEach((x) -> {
                x.put("index", index.getAndIncrement());

                try {
                    MapWrapper<String, Object> mw = new MapWrapper((HashMap)x);
                    mw.forEach((y) -> {
                        if (y.getValue() instanceof Clob) {
                            y.setValue(StringUtil.clobStr((Clob)y.getValue()));
                        }

                    });
                    TemplateModel columnModel = build.wrap(x);
                    if (templateModels.length > 0) {
                        templateModels[0] = columnModel;
                    } else {
                        environment.setVariable(this.variableName, columnModel);
                    }

                    environment.setVariable("column", columnModel);
                    templateDirectiveBody.render(environment.getOut());
                    environment.setVariable("column", column);
                    environment.setVariable(this.variableName, oldVar);
                } catch (TemplateModelException var10) {
                    var10.printStackTrace();
                } catch (TemplateException var11) {
                    var11.printStackTrace();
                } catch (IOException var12) {
                    var12.printStackTrace();
                }

            });
        }
    }

    static {
        build = (new BeansWrapperBuilder(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS)).build();
    }
}
